const dayjs = require('dayjs');
// 时间相关的工具类
const dateHelper = require('../../../store/helpers/date_helper');

// 定义time_helper兼容层
const timeHelper = {
    // 获取时间戳
    time: function(fmt = '', timestamp = null) {
        if (!fmt) return Math.round(dayjs().valueOf() / 1000); // 返回时间戳
        
        return dateHelper.format(timestamp ? timestamp * 1000 : null, fmt);
    },

    // 时间戳转时间
    timestamp2Time: function(timestamp, fmt = 'YYYY-MM-DD HH:mm:ss') {
        if (!timestamp) return '';
        return dateHelper.format(timestamp * 1000, fmt);
    },

    // 时间转时间戳
    time2Timestamp: function(date) {
        if (!date) return 0;
        return Math.floor(dateHelper.getDayjs(date).valueOf() / 1000);
    },

    // 时间戳转相对时间
    timestame2Ago: function(timestamp) {
        return dateHelper.timeAgo(timestamp);
    },

    // 获取星期几
    week: function(date) {
        if (!date) return '';
        return dateHelper.weekday(date, true).replace('周', '星期');
    },

    // 获取月份第一天时间戳
    getMonthFirstTimestamp: function(timestamp = null) {
        const date = timestamp ? timestamp * 1000 : null;
        return Math.floor(dateHelper.startOf(date, 'month').getTime() / 1000);
    },

    // 获取月份最后一天时间戳
    getMonthLastTimestamp: function(timestamp = null) {
        const date = timestamp ? timestamp * 1000 : null;
        return Math.floor(dateHelper.endOf(date, 'month').getTime() / 1000);
    },

    // 获取某天开始时间戳
    getDayFirstTimestamp: function(timestamp = null) {
        const date = timestamp ? timestamp * 1000 : null;
        return Math.floor(dateHelper.startOf(date, 'day').getTime() / 1000);
    },

    // 获取年龄
    getAge: function(timestamp) {
        if (!timestamp) return '';
        return dateHelper.diff(dayjs(), timestamp * 1000, 'year');
    },

    // 获取今天开始的最小时间戳
    getNowMinTimestamp: function() {
        return this.getDayFirstTimestamp();
    },

    // 简单格式化日期
    simpleDate: function(timestamp = null) {
        return dateHelper.format(timestamp ? timestamp * 1000 : null, 'YYYY.MM.DD');
    },

    // 中文格式化日期
    fmtDateCHN: function(timestamp = null) {
        return dateHelper.formatCN(timestamp ? timestamp * 1000 : null, false);
    },

    // 中文格式化日期（别名）
    fmtDateCN: function(timestamp = null) {
        return this.fmtDateCHN(timestamp);
    },

    // 中文格式化日期时间
    fmtDateTimeCN: function(timestamp = null) {
        return dateHelper.formatCN(timestamp ? timestamp * 1000 : null, true);
    },

    // 获取时间剩余
    getTimeLeft: function(second) {
        if (!second) return '';
        
        const days = Math.floor(second / 86400);
        const hours = Math.floor((second % 86400) / 3600);
        const minutes = Math.floor((second % 3600) / 60);
        const seconds = second % 60;
        
        if (days > 0)
            return `${days}:${hours}:${minutes}:${seconds}`;
        else if (hours > 0)
            return `${hours}:${minutes}:${seconds}`;
        else if (minutes > 0)
            return `${minutes}:${seconds}`;
        else
            return seconds.format("YYYY-MM-DD HH:mm:ss");
    }
};

// 导出兼容层
module.exports = timeHelper; 